package com.woniuxy.shop.servelet.front;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.woniuxy.shop.entity.CateGory;
import com.woniuxy.shop.entity.Goods;
import com.woniuxy.shop.service.CateGoryService;
import com.woniuxy.shop.service.GoodsService;
import com.woniuxy.shop.service.ServiceProxyFactory;

/**
 * Servlet implementation class DetailGoods
 */
@WebServlet("/detailgoods")
public class DetailGoods extends HttpServlet {
	private static final long serialVersionUID = 1L;
	GoodsService gs = ServiceProxyFactory.getProxy(GoodsService.class);
    CateGoryService cs = new CateGoryService();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String opr = request.getParameter("opr");
		if("query".equals(opr)) {
			doQuery(request,response);
		}
	}

	/**
	 * 根据商品ID查询商品
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			//查询当前商品
			int goodsid = Integer.parseInt(request.getParameter("goodsid"));
			Goods goods = gs.getGoods(goodsid);
			//查询热卖商品
			List<Goods> hotGoods = gs.getHotGoods();
			List<CateGory> categories = cs.getCateGory();
			request.setAttribute("hotGoods", hotGoods);
			request.setAttribute("goods", goods);
			request.setAttribute("categories", categories);
			request.getRequestDispatcher("goods_view.jsp").forward(request, response);
		} catch (Exception e) {
			//待进一步完善，没有给出错误的提示
			response.sendRedirect("index");
		}
		
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
